(function() {


    angular.module('BlurAdmin.pages.console')
        .controller('statisticsCtrl', statisticsCtrl);

    function statisticsCtrl($scope, tongxin, $http, $myconfig, $datepicker, $mydialog) {
        //饼状图数据
        $scope.tpo = 1;
        $scope.myTabs = function(val) {
            $scope.tpo = val;
        }
        $scope.dat = {
            dat1: $datepicker.dataNew(),
            dat2: $datepicker.dataNew(),
        };
        $scope.dat.dat1.date_ = '';
        $scope.dat.dat2.date_ = '';

        var pie1 = echarts.init(document.getElementById('pie1'));
        var pie2 = echarts.init(document.getElementById('pie2'));

        var pie1_opt = { //金鼻光

            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                data: ['bar'],
                x: 'center',
                y: 'bottom',
            },
            series: [{
                name: '使用时间',
                type: 'pie',
                radius: '55%',
                data: [
                    { value: 235, name: '视频广告' },

                ]
            }]
        };

        var pie2_opt = { //换颜宝
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                data: ['bar'],
                x: 'center',
                y: 'bottom',
            },
            series: [{
                name: '使用时间',
                type: 'pie',
                radius: '55%',
                data: [
                    { value: 235, name: '视频广告' },

                ]
            }]
        };

        pie1.setOption(pie1_opt);
        pie2.setOption(pie2_opt);



        //请求数据
        $scope.is_jbg = true;

        function jbg_shebie() { //金鼻光
            var data1 = document.getElementById("dat1") ? document.getElementById("dat1").value : null;
            var data2 = document.getElementById("dat2") ? document.getElementById("dat2").value : null;
            var opt = {
                "dev_name": "金鼻光",
                "start_time": data1,
                "end_time": data2

            }
            $http.get($myconfig.api.Physicallogs_controls_mode, { params: opt }).then(function(res) {
                // { x: '暂无数据', y: 0 }
                var arr = [];
                var name = [];
                for (var i = 0; i < res.data.data.length; i++) {
                    arr.push({});
                    name.push(res.data.data[i].mode.replace(/\s/, ""));
                    arr[i].name = res.data.data[i].mode.replace(/\s/, "");
                    arr[i].value = ~~(res.data.data[i].durations / 60);
                }
                $scope.is_jbg = true;
                $scope.jbg_1 = false;
                if (res.data.data.length == 0) {
                    arr[{ name: '暂无数据', value: 100 }];
                    $scope.is_jbg = false;
                } else if (res.data.data.length == 1) {
                    $scope.is_jbg = false;
                    $scope.jbg_1 = true;
                    $scope.jbg_1_data = { name: arr[0].name, val: arr[0].value };
                }

                pie1_opt.legend.data = name;
                pie1_opt.series[0].data = arr;
                pie1.setOption(pie1_opt);


                pie1.dispose();
                pie1 = echarts.init(document.getElementById('pie1'));
                pie1_opt.legend.data = name;
                pie1_opt.series[0].data = arr;
                setTimeout(function() {
                    pie1.setOption(pie1_opt);
                }, 20);
                pie1.setOption(pie1_opt);

            })
        }

        $scope.is_hyb = true;

        function hyb_shebie() { //焕颜宝
            var data1 = document.getElementById("dat1") ? document.getElementById("dat1").value : null;
            var data2 = document.getElementById("dat2") ? document.getElementById("dat2").value : null;
            var opt = {
                "dev_name": "焕颜宝",
                "start_time": data1,
                "end_time": data2
            }
            $http.get($myconfig.api.Physicallogs_controls_mode, { params: opt }).then(function(res) {
                var arr = [];
                var name = [];
                for (var i = 0; i < res.data.data.length; i++) {
                    arr.push({});
                    name.push(res.data.data[i].mode.replace(/\s/, ""));
                    arr[i].name = res.data.data[i].mode.replace(/\s/, "");
                    arr[i].value = ~~(res.data.data[i].durations / 60);
                }
                $scope.is_hyb = true;
                $scope.hyb_1 = false;
                if (res.data.data.length == 0) {
                    $scope.is_hyb = false;
                    arr[{ name: '暂无数据', value: 100 }]
                } else if (res.data.data.length == 1) {
                    $scope.is_hyb = false;
                    $scope.hyb_1 = true;
                    $scope.hyb_1_data = { name: arr[0].name, val: arr[0].value };
                }
                pie2_opt.legend.data = name;
                pie2_opt.series[0].data = arr;
                pie2.setOption(pie2_opt);

                pie2.dispose();
                pie2 = echarts.init(document.getElementById('pie2'));
                pie2_opt.legend.data = name;
                pie2_opt.series[0].data = arr;
                setTimeout(function() {
                    pie2.setOption(pie2_opt);

                }, 20);
            })
        }

        $scope.Search = function() { //使用情况
            var data1 = document.getElementById("dat1") ? document.getElementById("dat1").value : null;
            var data2 = document.getElementById("dat2") ? document.getElementById("dat2").value : null;
            var nwe_date = new Date();

            if (data2) {
                if (((new Date(data2)).getTime() + 86400000) > nwe_date.getTime()) {
                    return $mydialog.fail('结束日期应小于今天');
                }
            }

            if (data1) {
                if (((new Date(data1)).getTime() + 86400000) > nwe_date.getTime()) {
                    return $mydialog.fail('开始日期应小于今天');
                }
            }

            if (data1 && data2) {
                if (((new Date(data1)).getTime() + 86400000) >= ((new Date(data2)).getTime() + 86400000)) {
                    return $mydialog.fail('结束时间至少大于开始时开始一天');
                }
            }

            jbg_shebie();
            hyb_shebie();
        }
        $scope.Search();


        $scope.sheb_num = function() { //设备数量统计
            var data1 = document.getElementById("dat1") ? document.getElementById("dat1").value : null;
            var data2 = document.getElementById("dat2") ? document.getElementById("dat2").value : null;
            var opt = {
                "start_time": data1,
                "end_time": data2
            }
            $http.get($myconfig.api.Physicallogs_console_pie_chart, { params: opt })
                .then(function(res) {
                    var opt = res.data.date;

                    var pie3 = echarts.init(document.getElementById('pie3'));
                    var pie3_opt = {
                        tooltip: {
                            trigger: 'item',
                            formatter: "{a} <br/>{b} : {c} ({d}%)"
                        },
                        series: [{
                            name: '使用时间',
                            type: 'pie',
                            radius: '55%',
                            data: [
                                { value: parseInt(opt['0'].number), name: opt[0].devicename },
                                { value: parseInt(opt['1'].number), name: opt[1].devicename },
                            ]
                        }]
                    };

                    pie3.setOption(pie3_opt);

                });
        }

    }
})();